package com.lyd.feign.demo.api.v1;

import com.lyd.feign.demo.feign.client.TestFeignClient;
import com.lyd.feign.demo.feign.dto.TestDto;
import com.lyd.feign.demo.feign.dto.ValidParamDto;
import com.lyd.sweet.exception.SweetBasicException;
import com.lyd.sweet.model.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Files;
import java.nio.file.Paths;

@Api(tags = "DEMO测试API")
@RestController
@RequestMapping("/v1/test")
public class TestController {

    private final TestFeignClient testUserFeignClient;

    public TestController(TestFeignClient testUserFeignClient) {
        this.testUserFeignClient = testUserFeignClient;
    }

    @ApiOperation("测试使用Feign调用服务1")
    @GetMapping("/feign_req1")
    public Boolean reqForFeignTest1() {
        ValidParamDto dto = new ValidParamDto();
        dto.setBusiness("test");
        dto.setName("木木");
        dto.setCardId("123456789987654321");
        return this.testUserFeignClient.test(dto);
    }

    @ApiOperation("测试使用Feign调用服务2")
    @GetMapping("/feign_req2")
    public Boolean reqForFeignTest2() {
        ValidParamDto dto = new ValidParamDto();
        dto.setBusiness("test");
        dto.setName("木木");
        dto.setCardId("123456789987654321");
        return this.testUserFeignClient.test2(dto);
    }

    @ApiOperation("测试使用Feign调用服务3")
    @GetMapping("/feign_req3")
    public Object reqForFeignTest3() {
        return this.testUserFeignClient.test3();
    }

    @ApiOperation("测试使用Feign调用服务4")
    @GetMapping("/feign_req4")
    public String reqForFeignTest4() {
        try {
            TestDto testDto = new TestDto();
            MockMultipartFile file = new MockMultipartFile("feign-pom.xml", "feign-pom.xml", "UTF-8",
                    Files.readAllBytes(Paths.get("/Users/apple/IdeaProjects/sweet-boot/sweet-boot-demo/feign-web-demo/pom.xml")));
            testDto.setFile(file);
            String result = this.testUserFeignClient.test10(testDto);
            return result;
        } catch (Exception e) {
            throw new SweetBasicException(e.getMessage());
        }
    }

    @ApiOperation("测试使用Feign调用服务5")
    @GetMapping("/feign_req5")
    public String reqForFeignTest5() {
        try {
            TestDto testDto = new TestDto();
            MockMultipartFile file = new MockMultipartFile("feign-pom.xml", "feign-pom.xml", "UTF-8",
                    Files.readAllBytes(Paths.get("/Users/apple/IdeaProjects/sweet-boot/sweet-boot-demo/feign-web-demo/pom.xml")));
            testDto.setFile(file);
            ResponseObject<String> responseObject = this.testUserFeignClient.test11(testDto);
            return responseObject.getData();
        } catch (Exception e) {
            throw new SweetBasicException(e.getMessage());
        }
    }

    @ApiOperation("测试使用Feign调用服务6")
    @GetMapping("/feign_req6")
    public Boolean reqForFeignTest6() {
        ValidParamDto dto = new ValidParamDto();
        dto.setBusiness("test");
        dto.setName("木木");
        dto.setCardId("123456789987654321");
        ResponseObject<Boolean> responseObject =  this.testUserFeignClient.test22(dto);
        return responseObject.getData();
    }
}
